package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class ShoppingCartServiceImpl implements ShoppingCartService {
    // 注入购物车Mapper
    private final ShoppingCartMapper shoppingCartMapper;
    // 注入菜品Mapper
    private final DishMapper dishMapper;
    // 注入套餐Mapper
    private final SetmealMapper setmealMapper;

    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //log.info("添加购物车：{}", shoppingCartDTO);

        // 创建购物车对象
        ShoppingCart shoppingCart=new ShoppingCart();
        // 将DTO中的属性复制到购物车对象中
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        // 获取当前用户ID
        Long userId= BaseContext.getCurrentId();
        // 设置购物车对象中的用户ID
        shoppingCart.setUserId(userId);

        // 根据购物车对象查询购物车列表
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        // 如果购物车列表不为空
        if ( list !=null && list.size()>0) {
            // 获取购物车列表中的第一个元素
            ShoppingCart cart = list.get(0);
            // 将购物车列表中的第一个元素的数量加1
            cart.setNumber(cart.getNumber()+1);
            // 更新购物车列表中的第一个元素的数量
            shoppingCartMapper.updateNumberById(cart);
        }else{

            // 获取菜品ID
            Long dishId = shoppingCartDTO.getDishId();
            // 如果菜品ID不为空
            if (dishId != null) {
                //当前为菜品
                // 根据菜品ID查询菜品
                Dish dish=dishMapper.getById(dishId);
                // 设置购物车对象中的菜品名称
                shoppingCart.setName(dish.getName());
                // 设置购物车对象中的菜品图片
                shoppingCart.setImage(dish.getImage());
                // 设置购物车对象中的菜品价格
                shoppingCart.setAmount(dish.getPrice());

//                shoppingCart.setNumber(1);
//                shoppingCart.setCreateTime(LocalDateTime.now());
            }else{
                //当前为套餐
                // 获取套餐ID
                Long setmealId = shoppingCartDTO.getSetmealId();
                // 根据套餐ID查询套餐
                Setmeal setmeal=setmealMapper.getById(setmealId);
                // 设置购物车对象中的套餐名称
                shoppingCart.setName(setmeal.getName());
                // 设置购物车对象中的套餐图片
                shoppingCart.setImage(setmeal.getImage());
                // 设置购物车对象中的套餐价格
                shoppingCart.setAmount(setmeal.getPrice());

//                shoppingCart.setNumber(1);
//                shoppingCart.setCreateTime(LocalDateTime.now());
            }
            // 设置购物车对象中的数量为1
            shoppingCart.setNumber(1);
            // 设置购物车对象中的创建时间为当前时间
            shoppingCart.setCreateTime(LocalDateTime.now());
            // 将购物车对象插入数据库
            shoppingCartMapper.insert(shoppingCart);
        }
    }



    public List<ShoppingCart> showShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart=ShoppingCart.builder()
                .userId(userId)
                .build();
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }


    public void cleanShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(userId);
    }

    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //设置查询条件，查询当前登录用户的购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());

        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        if(list != null && list.size() > 0){
            shoppingCart = list.get(0);

            Integer number = shoppingCart.getNumber();
            if(number == 1){
                //当前商品在购物车中的份数为1，直接删除当前记录
                shoppingCartMapper.deleteById(shoppingCart.getId());
            }else {
                //当前商品在购物车中的份数不为1，修改份数即可
                shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                shoppingCartMapper.updateNumberById(shoppingCart);
            }
        }
    }
}
